"""
  根据参数详细查询域名
"""
from django.db import connection, connections
from django.db.models import Q
import copy
import math
from trade import models
def fastSelectTbale():
  """
  快速查询映射
  :return:
  """
  table = {
    '1': '纯数字',
    '2': '2位数字',
    '3': '3位数字',
    '4': '纯字母',
    '5': '2个字母',
    '6': '3个字母',
    '7': '单拼',
    '8': '双拼',
    '9': '三拼',
    '10': '杂米',
    '11': '二杂',
    '12': '三杂',
    '13': '中文'
  }
  return table

def domainSuffix():
  """
  域名后缀映射
  :return:
  """
  table = {
    '0': '.com$',
    '1': '.cn$',
    '2': '.net$',
    '3': '.xin$',
    '4': '.top$',
    '5': '.biz$',
    '6': '.cc$',
    '7': '.tv$',
    '8': '.mobi$',
    '9': '.org$',
    '10': '.org.cn$',
    '11': '.wang$',
    '12': '.vip$',
    '13': '.xyz$',
    '14': '.club$',
    '15': '.shop$',
    '16': '.info$',
    '17': '.ltd$',
    '18': '.site$',
    '19': '.online$',
    '20': '.tech$',
    '21': '.store$',
    '22': '.group$',
    '23': '.red$',
    '24': '.kim$',
    '25': '.pro$',
    '26': '.name$',
    '27': '.com.cn$',
    '28': '.net.cn$',
    '29': '.ai$',
    '30': '.io$'
  }
  return table

def get_page_list(nowPage, totlePage):
  """
  获取一个页码表，渲染index页面
  :param nowPage: 当前的页码
  :param totlePage: 总页码
  :return: 返回一个页码列表
  """
  nowPage = int(nowPage)
  if nowPage < 5:
    if totlePage < 5:
      page_list = [i for i in range(1, totlePage+1)]
    else:
      page_list = [i for i in range(1,6)]
  elif nowPage >= totlePage - 2:
    page_list = [ totlePage-j  for j in range(4,-1,-1) ]
  else:
    page_list = [nowPage-k1 for k1 in range(3,1,-1)] + \
                [nowPage] + [nowPage+k2 for k2 in range(1,3)]
  return page_list

def domainKewWorld(text):
  """
  处理关键字
  :param text:
  :return:
  """
  keyWord_list = str(text).split(',')
  if len(keyWord_list) <= 0:
    keyWord_list = None
  return keyWord_list

def searchDoamin(fastSelect=None,kewWord=None,startEndKeyWord=None,\
                 suffix=None,buyType=None, pageNum=1, **kwargs):
  """
  查询函数
  :param fastSelect: 快速查询
  :param kewWord: 关键字
  :param excludekeyWord: 是否排除
  :param suffix: 后缀
  :param buyType: 购买类型
  :return: 处理完成的字典
  """
  fastSelectHandler = fastSelectTbale()
  suffixHandler = domainSuffix()
  kewWordList = domainKewWorld(kewWord)
  row = models.DomainList.objects
  if fastSelect in fastSelectHandler:
    fastSelect = fastSelectHandler.get(fastSelect)
    row = row.filter(domain_type__regex=fastSelect)
  if suffix in suffixHandler:
    suffix = suffixHandler.get(suffix)
    row = row.filter(domain_name__regex=suffix)
  if buyType:
    row = row.filter(transaction_type=int(buyType))
  if kewWord:
    if kewWordList[-1] == '':
      kewWordList.pop()
    if startEndKeyWord == 'start':
      kewWord_regex = '(^' + '|^'.join(kewWordList) + ')'
      row = row.filter(domain_name__regex=str(kewWord_regex))
    elif startEndKeyWord == 'end':
      kewWord_regex = '(' + '$|'.join(kewWordList) + '$)'
      row = row.filter(domain_name__regex=str(kewWord_regex))
    else:
      kewWord_regex = '(' + '|'.join(kewWordList) + ')'
      row = row.filter(domain_name__regex=str(kewWord_regex))
  totlePage = math.ceil(row.count() / 30)
  start = int(30*int(pageNum)-30)
  end =   int(30*int(pageNum))
  row = row.order_by('domain_name')[start:end]
  info = {}
  page_list = get_page_list(nowPage=pageNum, totlePage=totlePage)
  try:
    for fetone in row:
      domain_name = fetone.domain_name
      length = len(domain_name.split('.')[0])
      db_info = fetone.domain_info
      db_info_dic = eval(db_info)
      tmp_dic = {
        'id': fetone.id,
        'score': db_info_dic.get('评分'),
        'status': fetone.status,
        'length': length,
        'transcrtion_type': str(fetone.transaction_type),
        'price': db_info_dic.get('价格')
      }
      info[domain_name] = tmp_dic
    domain_info = {
      'nowPage': pageNum,
      'totlePage': totlePage,
      'info': info,
      'pageList': page_list
    }
  except:
    domain_info = 'error'
  return domain_info

